var mainPageLoaded = true;
var currentPage = 'menu';
var praticeTemplate = '<li class="menuItem buttonMenu"></li>';
var listOfAreas = {};
var directAccess = false;
var user;
/* ¤OFFLINE
var listOfBatteries = __batteries__;
¤OFFLINEEND */

/**
 * initialization
 */
$().ready(function(){
	/* ¤ONLINE */
	$.ajax({
		type: 'GET',
		url: serverPath + '/front/menu/',
		success: function(userData) {
			user = userData;
			loadUserData();
		},
		error: function(data) {
			window.location = indexPagePath;
		},
		dataType: 'json'
	});
	/* ¤ONLINEEND */
	/* ¤OFFLINE
	user = __userdata__;
	loadUserData();
	¤OFFLINEEND */
});

/**
 * Load recieved user data
 */
function loadUserData() {
	/* ¤ONLINE */
	var page = location.pathname.split('/').slice(-1)[0];
	if (page.indexOf(batterieAccessPrefix) == 0) {
		// Direct access to a batterie
		// get batterie ID
		directAccess = true;
		mainPageLoaded = false;
		var batId = page.split('_').slice(-1)[0];
		$.ajax({
			type: 'GET',
			url: serverPath + '/batterie/get?r=1&id=' + batId,
			success: function(batterie) {
				selectExercice(batterie);
			},
			error: function() {
				window.location = mainPagePath;
			},
			dataType: 'json'
		});
	}else{
		if (user.isGuest) {
			window.location = indexPagePath;
		}
		
		// load menu
		$.getJSON(serverPath + '/mainmenu/get?id=' + user.menu_id, function(data) {
			listOfAreas = data;
			
			loadListOfArea();
		});
	}
	/* ¤ONLINEEND */
	/* ¤OFFLINE
		listOfAreas = __listOfAreas__;
		loadListOfArea();
	¤OFFLINEEND */
}

function loadListOfArea() {
	// initialize classes for short keys
	var shortKey = 1;
	
	$.each(listOfAreas, function(idx, pratice){
		var praticeDom = $(praticeTemplate);
		praticeDom.addClass('shortKey' + shortKey++).append($('<div class="shortKeyIcon"></div>')).append($('<a/>').html(pratice.nom).attr('href', '#').click(
				function(){
					selectArea(idx);
					return false;
				}
		));
		var currentRow = parseInt((shortKey - 2) / 3);
		if ($(".menu.pratices div.rowContainer")[currentRow] === undefined) {
			$(".menu.pratices").append($("<div/>").addClass("rowContainer"));
		}
		$($(".menu.pratices div.rowContainer")[currentRow]).append(praticeDom);
	});
	showContainer('menuContainer');
}

/**
 * select the area (meta-type of exercice)
 */
function selectArea(areaId) {
	var area = listOfAreas[areaId];
	$('.menu.batteries').html('');
	
	// manage navigation
	if ($('body').hasClass('menuContainerDisplayed')) {
		$('.navigationArea .menuNavButton').html(area.nom);
		$('.navigationArea .menuNavButton').removeClass('hidden');
	}
	$('.navigationArea .batterieChoiceNavButton').addClass('hidden');
	$('.navigationArea .exerciceChoiceNavButton').addClass('hidden');
	
	// fill list
	$.each(area.sousmenu, fillBatterie);

	if ($(".menu.batteries li").length == 1) {
		// display batteries of the only existing exercice
		hasOnlyOneExercice = true;
		$(".menu.batteries li").find("a").trigger("click");
	} else {
		// display the right frame
		hasOnlyOneExercice = false;
		displayBatteries();
	}
}

/*
 * Actions 
 */

function back() {
	$(".actionLetsGo").blur();
	if (!$('.myAccountContainer').hasClass('hidden')) {
		displayMainMenu();
	}else if (!$('.batterieChoiceContainer').hasClass('hidden')) {
		displayMainMenu();
	}else if (!$('.exerciceChoiceContainer').hasClass('hidden')) {
		backFromExercices();
	}else if (!$('.serieChoiceContainer').hasClass('hidden')) {
		if (directAccess) {
			displayMainMenu();
		}else{
			backFromSeries();
		}
	}else if (!$('.wordChoiceContainer').hasClass('hidden')) {
		if (directAccess && hasOnlyOneSerie) {
			displayMainMenu();
		}else{
			backFromWords();
		}
	}else if (!$('.trainingContainer').hasClass('hidden')) {
		displayWords();
	}else {
		displayMainMenu();
	}
}
